package com.whq.thrift.api.web.base.serviceconf.db;

import org.apache.ibatis.jdbc.SqlBuilder;

import com.whq.common.StringUtil;

public class ModuleInfoSqlProvider {
	public String getSelectRecordsSql(String moduleId) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("*");
		SqlBuilder.FROM("t_module_info");

		if (StringUtil.isNotBlank(moduleId)) {
			SqlBuilder.WHERE("module_id like #{moduleId}");
		}

		SqlBuilder.ORDER_BY("module_id asc");
		return SqlBuilder.SQL() + " limit #{beginIndex}, #{maxCount} ";
	}

	public String getSelectCountSql(String moduleId) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("count(*)");
		SqlBuilder.FROM("t_module_info");

		if (StringUtil.isNotBlank(moduleId)) {
			SqlBuilder.WHERE("module_id like #{moduleId}");
		}

		return SqlBuilder.SQL();
	}

	public String getUpdateSql(ModuleInfo moduleInfo) {
		SqlBuilder.BEGIN();
		SqlBuilder.UPDATE("t_module_info");

		if (StringUtil.isNotBlank(moduleInfo.getComment())) {
			SqlBuilder.SET("comment = #{comment}");
		}

		if (StringUtil.isNotBlank(moduleInfo.getOwner())) {
			SqlBuilder.SET("owner = #{owner}");
		}

		if (moduleInfo.getUpdateTime() != null) {
			SqlBuilder.SET("update_time = #{updateTime}");
		}

		SqlBuilder.SET("for_gateway = #{forGateway}");
		SqlBuilder.SET("err_code_min = #{errCodeMin}");
		SqlBuilder.SET("err_code_max = #{errCodeMax}");
		SqlBuilder.SET("module_status = #{moduleStatus}");

		SqlBuilder.SET("service_port = #{servicePort}");

		if (moduleInfo.getModuleComment() != null) {
			SqlBuilder.SET("module_comment = #{moduleComment}");
		}

		SqlBuilder.WHERE("module_id=#{moduleId}");
		return SqlBuilder.SQL();
	}
}
